package xyz.xpman.prisonerfitness.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import xyz.xpman.prisonerfitness.R;
import xyz.xpman.prisonerfitness.cache.AsyncImageLoader;
import xyz.xpman.prisonerfitness.entity.Chapter;

@SuppressWarnings("unused")
@RequiresApi(api = Build.VERSION_CODES.O)
public class ChapterAdapter extends BaseAdapter {
    private final Activity context;
    private final int templateId;
    private final List<Chapter> items;
    private final LayoutInflater layoutInflater;


    public ChapterAdapter (@NotNull Activity context, int templateId, @NotNull List<Chapter> items){
        this.items = items;
        this.context = context;
        this.templateId = templateId;
        this.layoutInflater = LayoutInflater.from(context);
    }


    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("ViewHolder")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChapterCache chapterCache;

        if (null == convertView) {
            convertView = layoutInflater.inflate(templateId, parent, false);

            TextView title = convertView.findViewById(R.id.title);
            ImageView guideImg = convertView.findViewById(R.id.bg_img);
            TextView description = convertView.findViewById(R.id.description);

            chapterCache = new ChapterCache(title, description, guideImg);

            convertView.setTag(chapterCache);
        } else {
            chapterCache = (ChapterCache) convertView.getTag();
        }

        chapterCache.process((Chapter) getItem(position), context);

        return convertView;
    }



    private static class ChapterCache {
        private final TextView title;
        private final ImageView guideImg;
        private final TextView description;


        private ChapterCache(@NotNull TextView textView,
                             @NotNull TextView descriptionView,
                             @NotNull ImageView bgImgView) {

            this.title = textView;
            this.guideImg = bgImgView;
            this.description = descriptionView;
        }


        public void setTitle(String title) {
            this.title.setText(title);
            this.guideImg.setContentDescription(title);
        }

        public void setDescription(String description) {
            this.description.setText(description);
        }


        public void setGuidImg(int imgId) {
            this.guideImg.setImageResource(imgId);
        }

        public void setGuidImg(@NotNull String url) {
            AsyncImageLoader.load(guideImg, url);
        }

        public void setGuidImg(@NotNull String[] urls) {
            AsyncImageLoader.load(guideImg, urls);
        }





        public void process(@NotNull Chapter chapter, @NotNull Activity context) {
            setTitle(chapter.getTitle());
            setDescription(chapter.getDescription());


            guideImg.setContentDescription(chapter.getTitle());

            if (null != chapter.getGuideImgId()) {
                setGuidImg(chapter.getGuideImgId());
            } else if (chapter.hasGuidImg()) {
                setGuidImg(chapter.getGuideImg());
            }
        }

    }

}
